package com.maven.booksystem.service;

import com.maven.booksystem.constants.Constants;
import com.maven.booksystem.mapper.UserMapper;
import com.maven.booksystem.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;



@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public boolean checkUser(String username, String password, HttpSession session){
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userinfo=userMapper.getByUsername(username);
        if(userinfo==null) {
            return false;
        }
        if(userinfo.getUserName().equals(username)
                && userinfo.getPassword().equals(password)) {
            userinfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY, userinfo);
            return true;
        }
        return false;
    }
}
